package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
// import cn.dev33.satoken.annotation.SaCheckPermission; // Ensure this is commented
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.ElderBasicVo;
import org.dromara.nursing.domain.bo.ElderBasicBo;
import org.dromara.nursing.service.IElderBasicService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 老人基础信息
 *
 * @author Xue
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/elderBasic")
public class ElderBasicController extends BaseController {

    private final IElderBasicService elderBasicService;

    /**
     * 查询老人基础信息列表
     */
    // @SaCheckPermission("nursing:elderBasic:list") // 已移除权限验证
    @GetMapping("/list")
    public TableDataInfo<ElderBasicVo> list(ElderBasicBo bo, PageQuery pageQuery) {
        return elderBasicService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出老人基础信息列表
     */
    // @SaCheckPermission("nursing:elderBasic:export") // 已移除权限验证
    @Log(title = "老人基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ElderBasicBo bo, HttpServletResponse response) {
        List<ElderBasicVo> list = elderBasicService.queryList(bo);
        ExcelUtil.exportExcel(list, "老人基础信息", ElderBasicVo.class, response);
    }

    /**
     * 获取老人基础信息详细信息
     *
     * @param elderId 主键
     */
    // @SaCheckPermission("nursing:elderBasic:query") // 已移除权限验证
    @GetMapping("/{elderId}")
    public R<ElderBasicVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long elderId) {
        return R.ok(elderBasicService.queryById(elderId));
    }

    /**
     * 新增老人基础信息
     */
    // @SaCheckPermission("nursing:elderBasic:add") // 已移除权限验证
    @Log(title = "老人基础信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ElderBasicBo bo) {
        return toAjax(elderBasicService.insertByBo(bo));
    }

    /**
     * 修改老人基础信息
     */
    // @SaCheckPermission("nursing:elderBasic:edit") // 已移除权限验证
    @Log(title = "老人基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ElderBasicBo bo) {
        return toAjax(elderBasicService.updateByBo(bo));
    }

    /**
     * 删除老人基础信息
     *
     * @param elderIds 主键串
     */
    // @SaCheckPermission("nursing:elderBasic:remove") // 已移除权限验证
    @Log(title = "老人基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{elderIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] elderIds) {
        return toAjax(elderBasicService.deleteWithValidByIds(List.of(elderIds), true));
    }

    /**
     * 审核老人基础信息
     *
     * @param elderId 老人ID
     * @param reviewStatus 审核状态(1:通过 2:拒绝)
     */
    @Log(title = "老人信息审核", businessType = BusinessType.UPDATE)
    @PutMapping("/review/{elderId}/{reviewStatus}")
    public R<Void> reviewElderBasic(@NotNull(message = "老人ID不能为空") @PathVariable Long elderId,
                         @NotBlank(message = "审核状态不能为空") @PathVariable String reviewStatus) {
        return toAjax(elderBasicService.reviewElderBasic(elderId, reviewStatus));
    }

    /**
     * 根据姓名模糊查询老人
     *
     * @param name 老人姓名
     * @return 匹配的老人列表
     */
    @GetMapping("/eldersByName")
    public R<List<ElderBasicVo>> getEldersByName(@RequestParam(required = false) String name) {
        ElderBasicBo bo = new ElderBasicBo();
        bo.setElderName(name);
        // 只查询审核通过的老人
        bo.setReviewStatus("1");
        List<ElderBasicVo> elders = elderBasicService.queryList(bo);
        return R.ok(elders);
    }

    /**
     * 根据姓名模糊查询所有老人（用于联系人验证）
     *
     * @param name 老人姓名
     * @return 匹配的老人列表
     */
    @GetMapping("/allEldersByName")
    public R<List<ElderBasicVo>> getAllEldersByName(@RequestParam(required = false) String name) {
        // 使用专门的方法查询所有老人，不进行权限过滤
        List<ElderBasicVo> elders = elderBasicService.queryAllForVerification(name);
        return R.ok(elders);
    }
}
